package com.boil.qz.safekavass.web.controller;

import com.boil.pojo.Page2Vo;
import com.boil.pojo.ResultVo;
import com.boil.qz.safekavass.model.LearnFileData;
import com.boil.qz.safekavass.pojo.LearnFileDataVo;
import com.boil.qz.safekavass.service.LearnFileDataService;
import com.boil.qz.safekavass.web.request.param.AddLearnFileDataParam;
import com.boil.qz.safekavass.web.request.param.IdParam;
import com.boil.qz.safekavass.web.request.param.LearnFileDataListParam;
import com.boil.util.Constants;
import com.boil.util.OkBaseController;
import com.boil.util.Page2;
import com.boil.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * Created by ChenYong on 2017-04-15.
 * <br>
 * 学习资料 Controller。
 */
@Controller
@RequestMapping("/web/learn/file")
public class LearnFileDataController extends OkBaseController {
    /**
     * 注入资料 Service
     */
    @Autowired
    private LearnFileDataService learnFileDataService;

    /**
     * 资料列表页面。
     *
     * @param model 模型
     * @return 资料列表页面
     */
    @RequestMapping(method = RequestMethod.GET, value = "/list.html")
    public String listHtml(Model model) {
        return "/learn/file/list";
    }

    /**
     * 添加资料页面。
     *
     * @param model 模型
     * @return 添加资料页面
     */
    @RequestMapping(method = RequestMethod.GET, value = "/add.html")
    public String addHtml(Model model) {
        return "/learn/file/add";
    }

    /**
     * 获取资料列表 json。
     *
     * @param model  模型
     * @param param  资料列表的请求参数
     * @param errors 请求参数的校验结果
     * @return 资料列表 json
     */
    @RequestMapping(method = RequestMethod.GET, value = "/list.json")
    @ResponseBody
    public ResultVo listJson(Model model, @Valid LearnFileDataListParam param, BindingResult errors) {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();
        // 查询分页实例
        Page2<LearnFileDataVo> page2 = Utils.getPage2(request, param, Constants.ID_STR);
        page2.getParams().put("learnCreatePerson", param.getLearnCreatePerson());
        page2.getParams().put("learnName", param.getLearnName());
        page2.getParams().put("learnType", param.getLearnType());
        page2.getParams().put("startDate", param.getStartDate());
        page2.getParams().put("endDate", param.getEndDate());

        // 分页查询
        learnFileDataService.findInPage(page2);

        // 获取 DataTables 专用分页映射实例
        Page2Vo<LearnFileDataVo> page2Vo = Utils.getPage2Vo(param, page2);

        resultVo.setResult(page2Vo);

        return resultVo;
    }

    /**
     * 添加资料。
     *
     * @param model  模型
     * @param param  添加资料的请求参数
     * @param errors 请求参数的校验结果
     * @return 重定向到资料列表页面--->“redirect:/learn/file/list.html”
     */
    @RequestMapping(method = RequestMethod.POST, value = "/add.html")
    public String add(Model model, @Valid AddLearnFileDataParam param, BindingResult errors) {
        // 保存资料
        learnFileDataService.save(param);

        return "redirect:/web/learn/file/list.html";
    }

    /**
     * 获取资料详细页面。
     *
     * @param model  模型
     * @param param  ID 参数
     * @param errors 请求参数的校验结果
     * @return 资料详细页面
     */
    @RequestMapping(method = RequestMethod.GET, value = "/detail.html")
    public String detailHtml(Model model, @Valid IdParam param, BindingResult errors) {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();
        // 获取资料
        LearnFileData learnFileData = learnFileDataService.findById(param.getId());

        resultVo.setResult(learnFileData);

        model.addAttribute(Constants.RESULT_VO, resultVo);

        return "/learn/file/detail";
    }
}